package com.wlyy.bcwlw.sys.main.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wlyy.bcwlw.base.BaseController;
import com.wlyy.bcwlw.base.Cache;
import com.wlyy.bcwlw.common.tree.TreeNode;
import com.wlyy.bcwlw.common.utils.CookieUtils;
import com.wlyy.bcwlw.sys.SecurityConst;
import com.wlyy.bcwlw.sys.SecurityFacade;
import com.wlyy.bcwlw.sys.application.entity.ApplicationDTO;
import com.wlyy.bcwlw.sys.area.entity.AreaDTO;
import com.wlyy.bcwlw.sys.area.service.AreaService;
import com.wlyy.bcwlw.sys.main.service.MainIndexService;
@RemoteProxy
@Controller
public class MainIndexController extends BaseController{
	
	@Autowired
	private MainIndexService mainIndexService;
	@Autowired
	private AreaService areaService;
	
	@RequestMapping("main/index")
	public String mainIndex(HttpServletRequest request,HttpServletResponse response){
		List<ApplicationDTO> list = mainIndexService.getUserApps();
		Cache.cacheMap.put("appList", list);
		set(request, "appList", list);
		set(request,"myName",SecurityFacade.getUserProfile().getName());
		boolean isAdmin = SecurityFacade.hasRole(SecurityConst.ROLE_ADMIN_MARK);
		if(isAdmin){
			List<AreaDTO> areaList = areaService.getAreaList();
			if(areaList != null && areaList.size() !=0){
				if(StringUtils.isEmpty(SecurityFacade.getCurrentAreaId())){
					SecurityFacade.setCurrentAreaId(areaList.get(0).getId());
				}
				for(AreaDTO areaDTO:areaList){
					if(areaDTO.getId().equals(SecurityFacade.getCurrentAreaId())){
						set(request,"adminCurrentAreaName",areaDTO.getName());
						break;
					}
				}
				
			}
			set(request,"isAdmin",isAdmin);
			set(request,"adminCurrentAreaId",SecurityFacade.getCurrentAreaId());
			set(request,"areaList",areaList);
		}
		
		return "sys/main/index";
	}
	
	public TreeNode getOwnMenuTreeByAppId(String id){
		return mainIndexService.getOwnMenuTreeByAppId(id);
	}
	
	public void setAdminCurrentAreaId(String areaId){
		SecurityFacade.setCurrentAreaId(areaId);
	}
	
	@RequestMapping(value = "/404")
	public String error_404(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return "common/error/404";
	}
	
	@RequestMapping(value = "/500")
	public String error_500(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return "common/error/500";
	}
	
	@RequestMapping(value = "/forbidden")
	public String error_forbidden(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return "common/error/forbidden";
	}
	
	@RequestMapping(value = "/theme/{theme}")
	public String getThemeInCookie(@PathVariable String theme,HttpServletRequest request, HttpServletResponse response) throws Exception{
		if (StringUtils.isNotBlank(theme)){
			CookieUtils.setCookie(response, "theme", theme);
		}else{
			theme = CookieUtils.getCookie(request, "theme");
		}
		return "redirect:"+request.getParameter("url");
	}

}
